package com.ease.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author nathanleewei
 * 
 */
public abstract class DateFormatUtil extends Detect {

	private static final transient Log log = LogFactory.getLog(DateFormatUtil.class);

	public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";

	public static final String FORMAT_DATETIME_NUMBER_ = "yyyyMMddHHmmss";

	public static final String DATETIME_UNDERLINE_FORMAT = "yyyy-MM-dd_HH:mm:ss";

	public static final String DATETIME_FORMAT_MS = "yyyy-MM-dd HH:mm:ss.SSS";

	public static final String DATE_FORMAT = "yyyy-MM-dd";

	public static final String TIME_FORMAT = "HH:mm:ss";

	public static final String RFC3339_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

	private static ThreadLocal<DateFormat> datetimeFormat = new ThreadLocal<DateFormat>() {
		protected synchronized DateFormat initialValue() {
			return new SimpleDateFormat(FORMAT_DATETIME);
		}
	};
	private static ThreadLocal<DateFormat> datetimeNumberFormat = new ThreadLocal<DateFormat>() {
		protected synchronized DateFormat initialValue() {
			return new SimpleDateFormat(FORMAT_DATETIME_NUMBER_);
		}
	};

	private static ThreadLocal<DateFormat> datatimeUnderlineFormat = new ThreadLocal<DateFormat>() {
		protected synchronized DateFormat initialValue() {
			return new SimpleDateFormat(DATETIME_UNDERLINE_FORMAT);
		}
	};

	private static ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
		protected synchronized DateFormat initialValue() {
			return new SimpleDateFormat(DATE_FORMAT);
		}
	};

	private static ThreadLocal<DateFormat> timeFormat = new ThreadLocal<DateFormat>() {
		protected synchronized DateFormat initialValue() {
			return new SimpleDateFormat(TIME_FORMAT);
		}
	};

	private static ThreadLocal<DateFormat> rfc3339DateFormat = new ThreadLocal<DateFormat>() {
		protected synchronized DateFormat initialValue() {
			return new SimpleDateFormat(RFC3339_FORMAT);
		}
	};

	private static DateFormat getDatetimeFormat() {
		return (DateFormat) datetimeFormat.get();
	}

	private static DateFormat getDatatimeNumberFormat() {
		return (DateFormat) datetimeNumberFormat.get();
	}

	private static DateFormat getDatetimeUnderlineFormat() {
		return (DateFormat) datatimeUnderlineFormat.get();
	}

	private static DateFormat getDateFormat() {
		return (DateFormat) dateFormat.get();
	}

	private static DateFormat getTimeFormat() {
		return (DateFormat) timeFormat.get();
	}

	private static DateFormat getRfc3339DateFormat() {
		return (DateFormat) rfc3339DateFormat.get();
	}


	public static String toDatetimeString(Date date) {
		return getDatetimeFormat().format(date);
	}

	public static String toDatatimeNumberString(Date date) {
		return getDatatimeNumberFormat().format(date);
	}

	public static String toDatetimeUnderlineString(Date date) {
		return getDatetimeUnderlineFormat().format(date);
	}

	public static String toTimeString(Date date) {
		return getTimeFormat().format(date);
	}

	public static Date toDatetime(String string) {
		try {
			return getDatetimeFormat().parse(string);
		} catch (ParseException e) {
			if (log.isErrorEnabled()) {
				log.error(string, e);
			}
		}
		return null;
	}
	
	public static Date toDate(String string) {
		try {
			return getDateFormat().parse(string);
		} catch (ParseException e) {
			if (log.isErrorEnabled()) {
				log.error(string, e);
			}
		}
		return null;
	}

	public static Date toTime(String string) {
		try {
			return getTimeFormat().parse(string);
		} catch (ParseException e) {
			if (log.isErrorEnabled()) {
				log.error(string, e);
			}
		}
		return null;
	}

//	public static boolean isExtDate(String string) {
//		try {
//			getRfc3339DateFormat().parse(string);
//			return true;
//		} catch (ParseException e) {
//			return false;
//		}
//	}
//
//	public static Date parseExtDate(String string) {
//		try {
//			return getRfc3339DateFormat().parse(string);
//		} catch (ParseException e) {
//			if (log.isErrorEnabled()) {
//				log.error(string, e);
//			}
//		}
//		return null;
//	}

	public static String toDateString(Date date) {
		return getDateFormat().format(date);
	}

//	// 转化UTC时间格式字符串(2007-10-23T17:15:44.000Z)
//	public static Date parseUTCDate(String string) {
//		string = string.replace("Z", " UTC");
//		try {
//			return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse(string);
//		} catch (ParseException e) {
//			if (log.isErrorEnabled()) {
//				log.error(string, e);
//			}
//		}
//		return null;
//	}

	public static void main(String[] args) {
		System.out.println(DateFormatUtil.toTime("15:46:35").after(DateFormatUtil.toTime(DateFormatUtil.toTimeString(new Date()))));

	}
}
